#ifndef __COMMU_H__
#define __COMMU_H__
#include "gd32f10x.h"

#define COMM_BUFFSIZE		512

#define COMM_485_DIR_PORT	GPIOA
#define COMM_485_DIR_PIN	GPIO_PIN_7

#define COMM_DIR_SEND()		gpio_bit_set(COMM_485_DIR_PORT, COMM_485_DIR_PIN);
#define COMM_DIR_RECV()		gpio_bit_reset(COMM_485_DIR_PORT, COMM_485_DIR_PIN);

#define CMD_NAME_MAX_LENGTH	32

#define OTA_DATA_MAX_LENGTH 1024

typedef enum
{
	Cmd_Device_Get_Info = 0,	//获取设备信息
	Cmd_Device_Register,		//注册设备到网关
	Cmd_Device_UnRegister,		//从网关移除设备
    Cmd_Device_Reset,			//设备复位重启
	Cmd_Device_Factory,			//设备恢复出厂设置
	Cmd_Device_Ctrl,			//设备控制
	Cmd_Device_Get_Status,		//获取设备状态
	Cmd_Device_Upload,			//设备状态上报
	Cmd_Device_Set_Config,		//设备参数设置
	Cmd_Device_Get_Config,		//设备参数获取
	CMD_NUM,
}CmdType;

typedef struct 
{
	CmdType type;
	char name[CMD_NAME_MAX_LENGTH];
}CmdObj;

typedef struct 
{
	uint8_t magic[2];
	uint8_t crc[2];
	uint32_t blockNum;
	uint16_t dataLen;
	uint8_t reserved[2];
}OTAInfo;

typedef struct 
{
	OTAInfo info;
	uint32_t fileSize;
	uint32_t blockSize;
	uint32_t receivedFileSize;
	uint8_t data[OTA_DATA_MAX_LENGTH];
	uint8_t verMajor;
	uint8_t verMinor;
	uint8_t verPatch;
}OTAObj;


struct COMM
{
	uint8_t buf[COMM_BUFFSIZE];
	uint16_t len;
};
void comm_uart_init(void);
void comm_send_bytes(uint8_t *buf, uint16_t len);
void comm_send_factory(void);
void comm_send_key_state(uint8_t keyId, uint8_t State);
void comm_receiver_process(void);

#endif
